<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="考核期">
               <a-select  v-model="queryParam.indexYearMonth" placeholder="请选择考核期">
              <a-select-option v-for="date in indexYearMonths"
                               :key="date.value">{{ date.text }}</a-select-option>
            </a-select>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="reload" @click="creator()">重新生成所有绩效报表</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{fixed:true,selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :scroll="tableScroll"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <!-- <a @click="handleEdit(record)">查看</a> -->

          <a-divider type="vertical" v-if="record.reportStatus == '0' || record.reportStatus == '1' || record.reportStatus == '3' || record.reportStatus == '4'"/>
          <a v-if="record.reportStatus == '0' || record.reportStatus == '1' || record.reportStatus == '3' || record.reportStatus == '4'" @click="creator(record)"">刷新报表</a>
          <a-divider type="vertical"  v-if="record.reportStatus == '3' || record.reportStatus == '4'" />
          <a v-if="record.reportStatus == '3' || record.reportStatus == '4'" @click="exportReport(record)"">下载报表</a>
        </span>

      </a-table>
    </div>

    <tkIndexReport-modal ref="modalForm" @ok="modalFormOk"></tkIndexReport-modal>
  </a-card>
</template>

<script>
  import {getYearMonthList,exportReport} from '@/api/api.js'
  /* 封装通用 api 服务 */
  import { apiService } from '@/api/api.service.js'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import TkIndexReportModal from './report/TkIndexReportModal'
  import { deleteAction, getAction,downFile,httpAction } from '@/api/manage'

  export default {
    name: "TkIndexReportList",
    mixins:[JeecgListMixin],
    components: {
      TkIndexReportModal
    },
    data () {
      return {
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'报表名称',
            align:"center",
            dataIndex: 'reportName'
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'reportStatus',
            customRender:function (text) {
              if(text == '0'){
                  return '有错误'
              }else if(text == '1'){
                  return '已就绪'
              }else if(text == '2'){
                  return '生成中'
              }else if(text == '3'){
                  return '已生成'
              }else{
                  return '已下载'
              }
            }
          },
          {
            title:'下载次数',
            align:"center",
            dataIndex: 'downloadTimes'
          },
          {
            title:'报表生成时间',
            align:"center",
            dataIndex: 'reportDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'最近一次下载时',
            align:"center",
            dataIndex: 'downloadDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"left",
            fixed:"right",
            width:250,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/api/pms/report/list",
          creator: "/api/pms/report/creator",
          exportXlsx: "/api/pms/report/exportXlsx",
        },
        queryParam:{
            indexYearMonth:'',
            id:'',
            reportCode:''
        },
        indexYearMonths:[{
          value:'',
          text:'请选择'
        }],
        tableScroll:{x :5*147+50}
      }
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    created() {
      apiService(getYearMonthList()).then((res) => {
          let result = res.result || {};
          this.indexYearMonths = result;
          this.queryParam.indexYearMonth = this.indexYearMonths[0].value;
          }).catch((error) => {
            this.$message.error(error.message);
      });                                                                                  
    },
    methods: {
      creator(report){
          let param = {
            indexYearMonth : this.queryParam.indexYearMonth,
            reportCode : report != null ? report.reportCode : null
          }
          httpAction(this.url.creator,param,'post').then((res)=>{
              if(res.success){
                this.$message.success(res.message);
                this.searchQuery();
                this.$emit('ok');
              }else{
                this.$message.warning(res.message);
              }
            })
      },
      exportReport(report){
        let fileName = report.reportName;
        let param = {
          indexYearMonth : this.queryParam.indexYearMonth,
          reportCode : report != null ? report.reportCode : null
        }
        if(this.selectedRowKeys && this.selectedRowKeys.length>0){
          param['selections'] = this.selectedRowKeys.join(",")
        }
        console.log("导出参数",param)
        downFile(this.url.exportXlsx,param).then((data)=>{
          if (!data) {
            this.$message.warning("文件下载失败")
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([data]), fileName+'.xls')
          }else{
            let url = window.URL.createObjectURL(new Blob([data]))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName+'.xlsx')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link); //下载完成移除元素
            window.URL.revokeObjectURL(url); //释放掉blob对象
            this.searchQuery();
          }
        })
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>